/**
 * 
 */
$(function() {
	_fmOpt = {
		partner : 'faxsun',
		appName : 'faxsun_web',
		token : getCSRFToken(),
	};
	var cimg = new Image(1, 1);
	cimg.onload = function() {
		_fmOpt.imgLoaded = true;
	};
	cimg.src = "https://fp.fraudmetrix.cn/fp/clear.png?partnerCode=faxsun&appName=faxsun_web&tokenId="
			+ _fmOpt.token;
	var fm = document.createElement('script');
	fm.type = 'text/javascript';
	fm.async = true;
	fm.src = ('https:' == document.location.protocol ? 'https://' : 'http://')
			+ 'static.fraudmetrix.cn/fm.js?ver=0.1&t='
			+ (new Date().getTime() / 3600000).toFixed(0);
	var s = document.getElementsByTagName('script')[0];
	s.parentNode.insertBefore(fm, s);

	// 显示错误信息
	var isError = getUrlParam("error");
	if (isError) {
		var msg = getUrlParam("msg");
		if (msg !== "undefineded" && msg !== "" && msg !== null) {
			$("#errorTips").children("span").children("p").children("span")
					.text(msg);
		}
	}

	/**
	 * 获取csrfToken
	 */
	function getCSRFToken() {
		var token = document.getElementsByName("csrfToken")[0];
		return token.getAttribute("value");
	}

	function getUrlParam(name) {
		// 构造一个含有目标参数的正则表达式对象
		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
		// 匹配目标参数
		var r = window.location.search.substr(1).match(reg);
		// 返回参数值
		if (r != null)
			return unescape(decodeURIComponent(r[2]));
		return null;
	}

	// 登录框焦点失去事件
	$("input[name='j_username']").blur(function(event) {
		$.ajax({
			url : buildHost() + '/shop/fraud/loginFraud',
			dataType : 'json',
			data : $('form').serialize(),
			method : 'POST'
		}).done(function(data) {
			if (data.action !== "undefinded") {
				var action = data.action;
				if (action === "SMS") {
					showSMSCaptcha();
				} else if (action === "VERIFICATION_CODE") {
					showCaptcha();
				}
			} else {
				location.relaod();
			}
		});
	});

	// 注册页面焦点失去事件
	$("input[name='customer.emailAddress']").blur(function(event) {
		$.ajax({
			url : buildHost() + '/shop/fraud/registerFraud',
			dataType : 'json',
			data : $('form').serialize(),
			method : 'POST'
		}).done(function(data) {
			if (data.action !== "undefinded") {
				var action = data.action;
				if (action === "SMS") {
					showSMSCaptcha();
				} else if (action === "VERIFICATION_CODE") {
					showCaptcha();
				}
			} else {
				location.relaod();
			}
		});
	});

	function buildHost() {
		var protocol = 'https:' == document.location.protocol ? 'https://'
				: 'http://';
		url = document.location.href;
		url = url.substring(protocol.length, url.length);
		var index = url.indexOf("/");
		return protocol + url.substring(0, index);
	}

	// 显示验证码区域
	function showCaptchaDiv() {
		$("#catchaDiv").show();
	}

	function showSMSCaptcha() {
		showCaptchaDiv();
		$("#catchaMobileDiv").show();
		$("#captcha_btn").show();
	}
	function showCaptcha() {
		showCaptchaDiv();
		$("#imgCaptchaArea").show();
	}

	// 点击验证码
	$("#captcha_img").click(function(event) {
		this.src = this.src;
	});
});
